<?php

namespace App\Api\Controllers;

use App\Api\Requests\RechargeRequest;
use App\Models\Recharge;
use Illuminate\Http\Request;
use Pay;

class RechargesController extends Controller
{
    public function store(Recharge $recharge,RechargeRequest $request){
        $recharge->fill($request->all());
        $recharge->user_id = $this->user()->id;
        $recharge->no = 'CZ'.date("YmdHis").rand(1000,999);
        $recharge->save();

//        #发起支付
        $order = [
            'out_trade_no' => $recharge->no,
            'total_amount' => $recharge->amount,
            'subject' => '充值',
        ];
        $pay = Pay::alipay()->app($order);
        $arr = explode('&',$pay->getContent());
        $r = [];
        foreach($arr as $v){
        	$c = strpos($v,"=");
        	$key = substr($v,0,$c);
        	$value = substr($v,$c+1);
            $r[$key] = $value;
        }
        $arr = json_encode($r);
        $recharge->pay1 = $arr;
        $recharge->pay = $pay->getContent();
        return $this->success($recharge);
    }



    public function index(Recharge $recharge){
        return $this->success($recharge->where('user_id',$this->user()->id)->orderBy('id','desc')->get());
    }
}
